package com.umeox.watch.moto.launcher.ui;

import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import com.umeox.moto.watch.themes.app.ThemeAppCompatActivity;
import com.umeox.watch.moto.dataservice.DataProvider;
import com.umeox.watch.moto.launcher.R;
import com.umeox.watch.moto.launcher.event.WatchFaceChangeEvent;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;


public class ThemeActivity extends ThemeAppCompatActivity implements View.OnClickListener {

    private static final int REQUEST_CODE = 101;
    private TextView mCurrentThemeTv;
    private String[] mThemeName;
    private TextView mWatchFaceNameTv;
    private static final String TAG = "ThemeActivity";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_theme);
        mThemeName = getResources().getStringArray(R.array.theme_name_phone);
        mCurrentThemeTv = findViewById(R.id.current_theme_tv);
        mWatchFaceNameTv = findViewById(R.id.tv_watch_face_name);

        AsyncTask.execute(() -> {
            final int currentTheme = DataProvider.getCurrentTheme();
            final int wallpaper = DataProvider.getWallpaper();
            runOnUiThread(() -> {
                mCurrentThemeTv.setText(mThemeName[currentTheme]);
                if (mWatchFaceNameTv != null) {
                    mWatchFaceNameTv.setText(getString(R.string.watchface_name, wallpaper));
                    findViewById(R.id.ll_watch_face).setOnClickListener(this);
                }
            });
        });

        findViewById(R.id.choose_theme_ll).setOnClickListener(this);
        EventBus.getDefault().register(this);
    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == REQUEST_CODE && resultCode == RESULT_OK && data != null) {
            int currentTheme = DataProvider.getCurrentTheme();
            mCurrentThemeTv.setText(mThemeName[currentTheme]);
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onWatchFaceChangeEvent(WatchFaceChangeEvent event) {
        mWatchFaceNameTv.setText(getString(R.string.watchface_name, event.mPosition));
    }

    @Override
    public void onClick(View v) {
        final int id = v.getId();
        switch (id) {
            case R.id.ll_watch_face:
                startActivity(new Intent(ThemeActivity.this, WatchFaceActivity.class));
                break;
            case R.id.choose_theme_ll:
                startActivityForResult(new Intent(ThemeActivity.this, ThemeSettingActivity.class), REQUEST_CODE);
                break;
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }
}
